package com.mashibing.juc.c_025;

import java.io.IOException;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

public class T04_ConcurrentQueue {

	public static void main(String[] args) throws Exception {

		Queue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();

		long currentTimeMillis1 = System.currentTimeMillis();

//		for(int i=0; i<100*10000; i++) {
//			concurrentLinkedQueue.offer("c" + i);
//		}

		CountDownLatch countDownLatch = new CountDownLatch(100);
		for (int k=0; k<100; k++) {
			new Thread(() -> {
				for(int i=0; i<10000; i++) {
					concurrentLinkedQueue.offer("c" + i);
//					concurrentLinkedQueue.poll();
				}
				countDownLatch.countDown();
			}).start();
		}
		countDownLatch.await();
		System.out.println(System.currentTimeMillis() - currentTimeMillis1);
//		System.out.println(concurrentLinkedQueue);
		System.out.println(concurrentLinkedQueue.size());


		Queue<String> linkedBlockingQueue = new LinkedBlockingQueue<>();

		long currentTimeMillis2 = System.currentTimeMillis();

//		for(int i=0; i<100*10000; i++) {
//			linkedBlockingQueue.offer("c" + i);
//		}
		CountDownLatch countDownLatch2 = new CountDownLatch(100);
		for (int k=0; k<100; k++) {
			new Thread(() -> {
				for(int i=0; i<10000; i++) {
					linkedBlockingQueue.offer("l" + i);
//					linkedBlockingQueue.poll();
				}
				countDownLatch2.countDown();
			}).start();
		}
		countDownLatch2.await();
		System.out.println(System.currentTimeMillis() - currentTimeMillis2);
//		System.out.println(linkedBlockingQueue);
		System.out.println(linkedBlockingQueue.size());

	}
}
